var api = require("../../mars/services/api.js");
let that;
Component({
  pageLifetimes: {
    show() {
      if (typeof this.getTabBar === 'function' &&
        this.getTabBar()) {
        this.getTabBar().setData({
          selected: 1
        })
      }
      this.getRecentCommunityPunchRecord();
      clearInterval();
      setInterval(function() {
        that.getRecentCommunityPunchRecord();
      }, 1000 * 1000)
    }
  },
  data: {
    community: [],
    hasGroup: false,
    groupId: "",
    firstTapTimestamp:0,
  },
  methods: {
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
      that = this;
      that.getUserEventsByUserId();
    },
    doubleTap(e){
      if(this.data.firstTapTimestamp==0){
        this.setData({
          firstTapTimestamp: e.timeStamp
        });
      }else{
        if (e.timeStamp-this.data.firstTapTimestamp>350){
          this.setData({
            firstTapTimestamp: e.timeStamp
          });
        }else{
          // trigger event
          e.target = e.currentTarget;
          this.likeThisPunch(e);
          this.setData({
            firstTapTimestamp: 0
          });
        }
      }
    },
    
    navigateToInvitation(e){
      wx.navigateTo({
        url:"/pages/shareAndInvitation/shareAndInvitation?groupId="+that.data.groupId
      })
    },
    getRecentCommunityPunchRecord() {
      api.WxRequest(api.getCommunityArticles, {
        userId: wx.getStorageSync('user').id || 5
      }, (res) => {
        if (res.status) return;
        that.setData({
          community: res.data
        });
      });
    },
    getUserEventsByUserId() {
      let user = wx.getStorageSync('user');
      api.WxRequest(api.getGroupByUserId, {
        userId: user.id
      }, (res) => {
        if (res.status) return;
        that.setData({
          hasGroup: res.data ? true : false,
          groupId: res.data && res.data.id
        });
      });
    },
    navigateToManagement() {
      let user = wx.getStorageSync('user');
      let options = `?groupId=${that.data.groupId}`;
      wx.navigateTo({
        url: `/pages/groupStatistics/groupStatistics${options}`,
      });
    },
    // nevitageToPunchs() {
    //   wx.navigateTo({
    //     url: '/pages/calorDailyAttendance/calorDailyAttendance'
    //   });
    // },
    nevitageToPunchRecords() {
      wx.navigateTo({
        url: '/pages/punchRecords/punchRecords'
      });
    },
    nevitageToCommunityCalender() {
      let user = wx.getStorageSync('user');
      let opts = `?userId=${user.id}&userName=${user.user_name}&avatarUrl=${user.avatar_url}`;
      wx.navigateTo({
        url: '/pages/communityCalender/communityCalender' + opts
      });
    },
    navigateBack() {
      wx.reLaunch({
        url: '/pages/calorieIndex/calorieIndex',
      });
    },
    likeThisPunch(e) {
      let idx = e.target.dataset.idx;
      let communityItem = that.data.community[idx];
      let punchId = communityItem.id;
      let flag = !communityItem.iLike;
      let userId = wx.getStorageSync("user").id;

      wx.showLoading({
        title: '正在加载中...',
      })
      api.WxRequest(api.likeThisPunch, {
        userId,
        punchId,
        flag
      }, (res) => {
        wx.hideLoading();
        wx.showToast({
          title: '操作成功',
        });
        that.setData({
          [`community[${idx}].iLike`]: flag,
          [`community[${idx}].likeNumber`]: flag ? (communityItem.likeNumber + 1) : (communityItem.likeNumber - 1),
        })
      });
    },
    navigateToCreateGroups() {
      wx.navigateTo({
        url: '/pages/createGroup/createGroup',
      });
    }
  }
})